// 递归深度克隆
export  function deepClone(target){ 
    var reslut;
    if( typeof target  === 'object'){
        if(Array.isArray(target)){
            reslut = []
            for(let i in target){
                reslut.push(deepClone(target[i]))
            }
        }else if(target === null){
            reslut = target
        }else if(target.constructor === RegExp ){
            reslut = target
        }else if(target.constructor === Date){
            reslut = target
        }else{
            reslut = {}
            for(let i in target){
                reslut[i] = deepClone(target[i])
            }
        }
    }else{
        reslut = target;
    }
    return reslut;
}
//JSONClone
export function JSONClone(target){
     return JSON.parse(JSON.stringify(target));  
}
//通用固定参数科里化，第一个参数为执行函数，其余为执行函数的参数
export function curry(){
    const fn = arguments[0];
    const args = Array.prototype.slice.call(arguments,1);
    if(args.length >= fn.length){
        return fn.apply(this,args)
    }
    function _c(){
        args.push(...arguments);
        if(args.length >= fn.length){
            return fn.apply(this,args);
        }
        return _c;
    }
    return _c;
}
//通用不固定参数科里化，第一个参数为要执行的函数，XX.run代表运行要执行的函数
export function varCurry(){
    const fn = arguments[0];
    const args = Array.prototype.slice.call(arguments,1);
     function _c(){
        args.push(...arguments);
        return _c;
    }
    _c.run = function(){
        return fn.apply(this,args);
    } 

    return _c;
}
//防抖，fn为函数，t为时间毫秒
export function debounce(fn,t=500){
    var time = null;
    return function(){
        const args = arguments;
        time && clearTimeout(time);
        time = setTimeout(()=>{
            fn.apply(this,args);
        },t)
    }

}

//节流  fn 为执行函数，t为时间，isType为判断使用定时器防抖还是时间戳防抖，默认为时间戳防抖
export function throttle(fn,t=500,isType = false){
    var time = null;
    if(isType) {
        return function(){
            if(time) return;
            const args = arguments;
            time = setTimeout(()=>{
                fn.apply(this,args);
                time = null;
            },t)
        }
    }else {
        time = new Date();
        return function(){
            const now = new Date();
            if(now - time < t ) return;
            fn.apply(this,arguments);
            time = now;
        }
    }


}
//数组去重
export function arrRemoveDup(arr=[]){
    if(arr.length >= 2 && arr instanceof Array){
        const obj = {};
        for(let i in arr){
            if(typeof i == 'string') arr[i] = arr[i].trim();
            if( arr[i] && !obj[arr[i]]){
                obj[arr[i]] = true
            }
        }
        return Object.keys(obj);
    } else {
        return arr;
    }
}
//拦截器  
export function intercpt(fn,{ beforeCall = null,afterCall = null }){
    return function (...ags) {
        if (!beforeCall || beforeCall.apply(this,ags) !== false) {
            const res =  fn.apply(this, ags);
            if (afterCall) {
                return afterCall.call(this,res)
            }
            return res;
        }    
    } 
    
}
//函数提纯
export function batch(fn) {
    return function (subj, ...ags) {
        if (Array.isArray(subj)) {
            return subj.map(item => {
                fn.apply(this, item, ags);
            })
        }
        return fn.call(this,subj,...ags)
    }
}

